package com.singlemountaintech.haogongjiang.qfd.ui.main.fragment;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.framework.fragment.BaseFragmentPresenter;
import com.framework.http.DefaultTransformer;
import com.singlemountaintech.haogongjiang.R;
import com.singlemountaintech.haogongjiang.http.JsonApiWrapper;
import com.singlemountaintech.haogongjiang.http.ProgressSubscriber;
import com.singlemountaintech.haogongjiang.model.response.LoginModel;
import com.singlemountaintech.haogongjiang.qfd.ITabFragment;
import com.singlemountaintech.haogongjiang.qfd.constant.Keys;
import com.singlemountaintech.haogongjiang.qfd.ui.main.activity.MyPublishActivity;
import com.singlemountaintech.haogongjiang.qfd.ui.main.delegate.TabMainDelegate;
import com.singlemountaintech.haogongjiang.utils.LoginUtil;
import com.singlemountaintech.haogongjiang.utils.UiUtils;

/**
 * Created by LUOLI on 2016/11/2.
 */
public class TabMainFragment extends BaseFragmentPresenter<TabMainDelegate> implements ITabFragment, View.OnClickListener {

    private Button button;

    @Override
    protected Class<TabMainDelegate> getDelegateClass() {
        return TabMainDelegate.class;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        viewDelegate.get(R.id.btn_test).setOnClickListener(this);
        viewDelegate.get(R.id.btn_clear).setOnClickListener(this);
        viewDelegate.get(R.id.btn_login).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.btn_clear:
                LoginUtil.clearLoginMessAge();
                break;
            case R.id.btn_test:
                startActivity(new Intent(getContext(), MyPublishActivity.class));
                break;
        }
    }

    private void login() {
        JsonApiWrapper.getApiService().login("13121211891", "123456", false)
                .compose(new DefaultTransformer<LoginModel>())
                .subscribe(new ProgressSubscriber<LoginModel>(getActivity(), true,"正在登录..."){
                    @Override
                    public void onNext(LoginModel response) {
                        if (Keys.HTTP_20000 == response.status) {
                            LoginUtil.putToken(response.token);
                            LoginUtil.putRefreshToken(response.refreshToken);
                            UiUtils.showToast(null, "登录成功！");

                        } else if (!TextUtils.isEmpty(response.message)) {
                            if (TextUtils.equals(response.message, "没有找到")) {
                                UiUtils.showToast(null, "当前账号没有注册，请去注册！");
                            } else {
                                UiUtils.showToast(null, response.message);
                            }

                        }
                    }
                });
    }

    @Override
    public void onMenuItemClick() {
    }

    @Override
    public Fragment getFragment() {
        return this;
    }

    @Override
    public String getTitle() {
        return "main fragment";
    }
}
